package com.daykw.wkinfo;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import com.daykw.wkinfo.config.NutzDaoConfig;
import com.daykw.wkinfo.http.HttpBase;
import com.daykw.wkinfo.model.BizType;
import com.daykw.wkinfo.task.ArticleHtmlJob;
import com.daykw.wkinfo.task.ArticleJob;
import com.daykw.wkinfo.task.BizTypeJob;
import com.daykw.wkinfo.task.DateTypeJob;
import com.daykw.wkinfo.task.TopicTypeJob;
import com.daykw.wkinfo.utils.Const;
import com.daykw.wkinfo.utils.FileWrite;
import com.daykw.wkinfo.utils.SpringContextHolder;
import com.daykw.wkinfo.utils.TQueue;

@SpringBootApplication
public class StartApp {

	private static Logger logger = Logger.getLogger(StartApp.class);

	public static void main(String[] args) {
		SpringApplicationBuilder builder = new SpringApplicationBuilder(StartApp.class);

		builder.web(WebApplicationType.SERVLET).run();

		logger.info("##########spring boot 启动成功##########");
		Const c = SpringContextHolder.getBean(Const.class);

		
		if (c.getSearch_date()) {
			ScheduledExecutorService scheduled2 = Executors.newSingleThreadScheduledExecutor();
			scheduled2.scheduleAtFixedRate(new DateTypeJob(), 5, 30, TimeUnit.SECONDS);
		}

		
		if (c.getSearch_topic()) {

			ScheduledExecutorService scheduled3 = Executors.newSingleThreadScheduledExecutor();
			scheduled3.scheduleAtFixedRate(new TopicTypeJob(), 5, 30, TimeUnit.SECONDS);
		}
		if (c.getSearch_biz())

		{

			ScheduledExecutorService scheduled5 = Executors.newSingleThreadScheduledExecutor();
			scheduled5.scheduleAtFixedRate(new BizTypeJob(), 5, 30, TimeUnit.SECONDS);
		}

		if (c.getList_thread()) {
			ScheduledExecutorService scheduled4 = Executors.newSingleThreadScheduledExecutor();
			scheduled4.scheduleAtFixedRate(new ArticleJob(), 5, 30, TimeUnit.SECONDS);

		}

		if (c.getArticle_thread()) {
			ScheduledExecutorService scheduled5 = Executors.newSingleThreadScheduledExecutor();
			scheduled5.scheduleAtFixedRate(new ArticleHtmlJob(), 0, 8, TimeUnit.SECONDS);

		}

	}

	public static void doLogin() throws Exception {
		NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);
		List<Record> rs = dao.query("t_user", Cnd.wrap("order by rand() LIMIT 1"));
		Record r = rs.get(0);
		String user = r.getString("username");
		String password = r.getString("password");
		HttpBase http = new HttpBase();
		WebDriver wd = http.getFirefox();

		try {
			wd.get("https://law.wkinfo.com.cn/legislation/list?fq=promulgatingDate%C7%81%5B2017.08.07%20TO%202018.08.07%5D%C7%81%C7%82%E6%9C%80%E8%BF%911%E5%B9%B4&tip=");

			// alphalawyer
			// 0x010000008549222c03e05b0e560ebaf02eec1f9556bedd1cbca87cb59867485ab6fd6787c220305b1b6adbdd
			wd.findElement(By.cssSelector("span.result-login-btn")).click();
			Thread.sleep(1000);
			wd.findElement(By.id("login-username")).sendKeys(user);
			Thread.sleep(1000);
			wd.findElement(By.id("login-password")).sendKeys(password);
			wd.findElement(By.cssSelector("button.btn.btn-block.btn-submit")).click();
			Thread.sleep(1000);
			logger.info("登陸完畢");
			TQueue.getFQueue().add(wd);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void getType() {
		NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);

		try {
			String html = FileWrite.readFile("D:\\elong.html");
			Document doc = Jsoup.parse(html);

			Elements es = doc.select("ul>li.jstree-node");

			for (Element e1 : es) {
				String id1 = e1.attr("id");
				String name1 = e1.select("a").first().text();

				Elements li2s = e1.select("ul>li");
				for (Element e2 : li2s) {
					String id2 = e2.attr("id");
					String name2 = e2.select("a").first().text();
					Elements li3s = e2.select("ul>li");
					for (Element e3 : li3s) {
						String id3 = e3.attr("id");
						String name3 = e3.select("a").first().text();
						Elements li4s = e3.select("ul>li");
						for (Element e4 : li4s) {
							String id4 = e4.attr("id");
							String name4 = e4.text();

							BizType t = new BizType();
							t.setCreate_time(new Date());
							t.setCode(id4);
							t.setName(name4);
							t.setParent_code(id3);
							t.setLevel(4);
							try {
								dao.fastInsert(t);
							} catch (Exception e) {

							}
						}
						BizType t = new BizType();
						t.setCreate_time(new Date());
						t.setCode(id3);
						t.setName(name3);
						t.setParent_code(id2);
						t.setLevel(3);
						try {
							dao.fastInsert(t);
						} catch (Exception e) {

						}

					}
					BizType t = new BizType();
					t.setCreate_time(new Date());
					t.setCode(id2);
					t.setName(name2);
					t.setParent_code(id1);
					t.setLevel(2);
					try {
						dao.fastInsert(t);
					} catch (Exception e) {

					}
				}
				BizType t = new BizType();
				t.setCreate_time(new Date());
				t.setCode(id1);
				t.setName(name1);
				t.setParent_code(null);
				t.setLevel(1);
				try {
					dao.fastInsert(t);
				} catch (Exception e) {

				}
			}

		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

}
